//
//  LNHGoodsDetailInfoView.m
//  LNH
//
//  Created by cruise on 2018/2/6.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "LNHGoodsDetailInfoView.h"

#import "LNHGoodsDetailViewModel.h"

@interface LNHGoodsDetailInfoView()

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *salesLabel;
@property (nonatomic, strong) UILabel *locationLabel;
@property (nonatomic, strong) UILabel *expressLabel;

@end

@implementation LNHGoodsDetailInfoView

/**
 为视图绑定 viewModel
 
 @param viewModel 要绑定的ViewModel
 @param params 额外参数
 */
- (void)bindViewModel:(id <LMViewProtocol>)viewModel withParams:(NSDictionary *)params {
    
    if ([viewModel isKindOfClass:[LNHGoodsDetailViewModel class]]){
        
        LNHGoodsDetailViewModel *_viewModel = (LNHGoodsDetailViewModel *)viewModel;
        
        [[RACObserve(_viewModel, infoDict) filter:^BOOL(id  _Nullable value) {
            return value != nil;
        }] subscribeNext:^(NSDictionary *dict) {
           
            [self.nameLabel setText:dict[kPropertyDataKeyGoodsDetailName]];
            [self.priceLabel setText:dict[kPropertyDataKeyGoodsDetailPrice]];
            [self.salesLabel setText:dict[kPropertyDataKeyGoodsDetailSales]];
            [self.locationLabel setText:dict[kPropertyDataKeyGoodsDetailLocation]];
            [self.expressLabel setText:dict[kPropertyDataKeyGoodsDetailPostage]];
            
        }];
        
    }
}

/**
 初始化额外数据
 */
- (void)lm_initializeForView {
    
    
}

/**
 初始化视图
 */
- (void)lm_createViewForView {
    
//    self.layer.borderWidth = 1;
    
    [self setBackgroundColor:[UIColor whiteColor]];
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 0.1f;
    self.layer.shadowRadius = 2.f;
    self.layer.shadowOffset = CGSizeMake(0, -4);
    
    [self addSubview:self.nameLabel];
    [self addSubview:self.priceLabel];
    [self addSubview:self.salesLabel];
    [self addSubview:self.locationLabel];
    [self addSubview:self.expressLabel];
    
    [self setNeedsUpdateConstraints];
}

-(void) updateConstraints {
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
    }];
    
    [self.expressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.priceLabel.mas_bottom).offset(15);
        make.left.equalTo(self).offset(20);
        make.width.mas_equalTo((SCREEN_WIDTH-40)/3);
    }];
    
    [self.salesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.expressLabel.mas_top);
        make.left.equalTo(self.expressLabel.mas_right);
        make.width.mas_equalTo((SCREEN_WIDTH-40)/3);
    }];
    
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.expressLabel.mas_top);
        make.left.equalTo(self.salesLabel.mas_right);
        make.width.mas_equalTo((SCREEN_WIDTH-40)/3);
    }];
    
    [super updateConstraints];
    
}

#pragma mark - Getter

-(UILabel *) nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        [_nameLabel setTextColor:RGBCOLOR(35, 24, 21)];
        [_nameLabel setFont:[UIFont systemFontOfSize:16]];
    }
    return _nameLabel;
}

-(UILabel *) priceLabel {
    if (!_priceLabel) {
        _priceLabel = [UILabel new];
        [_priceLabel setTextColor:RGBCOLOR(241, 90, 36)];
        [_priceLabel setFont:[UIFont systemFontOfSize:14]];
    }
    return _priceLabel;
}

-(UILabel *) salesLabel {
    if (!_salesLabel) {
        _salesLabel = [UILabel new];
        [_salesLabel setTextColor:RGBCOLOR(158, 158, 158)];
        [_salesLabel setFont:[UIFont systemFontOfSize:14]];
        [_salesLabel setTextAlignment:NSTextAlignmentCenter];
    }
    return _salesLabel;
}

-(UILabel *) locationLabel {
    if (!_locationLabel) {
        _locationLabel = [UILabel new];
        [_locationLabel setTextColor:RGBCOLOR(158, 158, 158)];
        [_locationLabel setFont:[UIFont systemFontOfSize:14]];
        [_locationLabel setTextAlignment:NSTextAlignmentRight];
    }
    return _locationLabel;
}

-(UILabel *) expressLabel {
    if (!_expressLabel) {
        _expressLabel = [UILabel new];
        [_expressLabel setTextColor:RGBCOLOR(158, 158, 158)];
        [_expressLabel setFont:[UIFont systemFontOfSize:14]];
        [_expressLabel setTextAlignment:NSTextAlignmentLeft];
    }
    return _expressLabel;
}

@end
